#include <stdio.h>
#include <stdlib.h>
#include<allegro.h>



int main()
{
int i;
	BITMAP *buffer;
	BITMAP *image;
	BITMAP *background;
        /*SAMPLE *pas;*/
        BITMAP *background2;
        SAMPLE *music;
        Jimmy Jimmy;
        int son_music ;
        serpent serpent;
        int continuer=1;
        allegro_init();
        install_keyboard();
        
        
/*install_sound( DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
if(install_sound==NULL)
{
allegro_message("ERROR! %s",allegro_error);
}
music=load_wav("music.wav");
        if (!music) {
readkey();
return 0;
}
        set_volume(255, 255);
        son_music=play_sample(music,128,128,1000,0) ;
  */      
        if(install_keyboard() ==-1){
		allegro_message("ERROR! %s",allegro_error);
		return -1;
	}
        
        set_color_depth(16);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,800,600,0,0) !=0){/* la résolution de l'écran */
		set_gfx_mode(GFX_TEXT,0,0,0,0);
		allegro_message("unable to intialize video mode!\n%s\n",allegro_error);
	return 1;}
        buffer = create_bitmap(800,600);
       
        background=load_bitmap("./images/background3.bmp",NULL);
        background2=load_bitmap("./images/Background1.bmp",NULL);
	Jimmy.img_right[0]=load_bitmap("./images/right0.bmp",NULL);
	Jimmy.img_right[1]=load_bitmap("./images/right1.bmp",NULL);
	Jimmy.img_right[2]=load_bitmap("./images/right2.bmp",NULL);
	Jimmy.img_left[0]=load_bitmap("./images/left0.bmp",NULL);
	Jimmy.img_left[1]=load_bitmap("./images/left1.bmp",NULL);
	Jimmy.img_left[2]=load_bitmap("./images/left2.bmp",NULL);
        serpent.img_right1=load_bitmap("./images/serpent.bmp",NULL);
        for(i=0;i<9;i++){
		if (Jimmy.img_right[i]==NULL && background2==NULL && Jimmy.img_left[i]==NULL )
		{allegro_message("could not load images");
	 	allegro_exit();}}
	Jimmy.x=SCREEN_W/2;
	Jimmy.y=SCREEN_H/2+200;
        serpent.x1=SCREEN_W/2+170;
	serpent.y1=SCREEN_H/2+200;
	
/****A déplacer sous les conditions if 
	menu.x=SCREEN_W/2;
	menu.y=SCREEN_H/2;*****/
    
	blit(background,buffer,0,0,0,0,background->w,background->h);
        blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
       

readkey();	
       while(continuer!=0)
{
if(key[KEY_ESC])
{
continuer=0;
}
if(key[KEY_1])
{

blit(background2,buffer,0,0,0,0,background2->w,background2->h);
blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);

	draw_sprite(buffer,Jimmy.img_right[0],Jimmy.x,Jimmy.y);
        draw_sprite(buffer,serpent.img_right1,serpent.x1,serpent.y1);
        
	blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);}
	i=1;
if (key[KEY_RIGHT] && Jimmy.x < SCREEN_W -50)
		{i++;if(i>3){i=0;}
		 image=Jimmy.img_right[i];
		 
		 blit(background2,buffer,0,0,0,0,background2->w,background2->h);
		 draw_sprite(buffer,image,Jimmy.x,Jimmy.y);
		 blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 Jimmy.x += 1;
		}
		 if (key[KEY_LEFT] && Jimmy.x > 0)
		{
		
		 image=Jimmy.img_left[i];
		 i++;
		 blit(background2,buffer,0,0,0,0,background2->w,background2->h);
		 draw_sprite(buffer,image,Jimmy.x,Jimmy.y);
		 blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 Jimmy.x -= 1;}
		if(i>3){i=0;}
 if (key[KEY_UP] && Jimmy.y > 0)
		{
		if(Jimmy.y-=1 )
                
                 
                {

		 image=Jimmy.img_left[0];
		 
		 blit(background2,buffer,0,0,0,0,background2->w,background2->h);
		 draw_sprite(buffer,image,Jimmy.x,Jimmy.y);
		 blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
}

 
}

}
destroy_bitmap(buffer);
destroy_bitmap(screen);


return 0;
}
